package com.nutiteq.multistored;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.nutiteq.BasicMapComponent;
import com.nutiteq.components.WgsPoint;
import com.nutiteq.controls.ControlKeys;
import com.nutiteq.fs.JSR75FileSystem;
import com.nutiteq.listeners.MapListener;
import com.nutiteq.maps.MultiStoredMap;
import com.nutiteq.ui.DefaultCursor;
import com.nutiteq.ui.DefaultZoomIndicator;
import com.nutiteq.ui.StringCopyright;
import com.nutiteq.ui.ThreadDrivenPanning;

public class MultiStoredCanvas extends Canvas implements MapListener {
  private BasicMapComponent mapComponent;
  private MultiStoredMap sm;

  public void initialize() {
    mapComponent = new BasicMapComponent("abcdtrial", MultiStoredSample.instance, getWidth(),
        getHeight(), new WgsPoint(8.377075,48.788771), 15);
    mapComponent.setFileSystem(new JSR75FileSystem());
    sm = new MultiStoredMap("OpenCycleMap", "root1/maps",new StringCopyright("OpenStreetMap"));
    
    final Image missing = Image.createImage(sm.getTileSize(), sm.getTileSize());
    final Graphics graphics = missing.getGraphics();
    graphics.setColor(0xFFCCCECC);
    graphics.fillRect(0, 0, sm.getTileSize(), sm.getTileSize());
    sm.setMissingTileImage(missing);

    sm.setMaxZoom(19);
    
    mapComponent.setMap(sm);
      
    System.out.println("Zoomrange " + mapComponent.getZoomRange().getMinZoom()+ " to "+ mapComponent.getZoomRange().getMaxZoom() );
//    System.out.println("ZoomLevels len "+sm.getSupportedZoomLevels().length);
    mapComponent.setPanningStrategy(new ThreadDrivenPanning());

    mapComponent.defineControlKey(ControlKeys.ZOOM_IN_KEY, KEY_POUND);
    mapComponent.defineControlKey(ControlKeys.ZOOM_OUT_KEY, KEY_STAR);
    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, KEY_NUM2);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, KEY_NUM8);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, KEY_NUM4);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, KEY_NUM6);

    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, -1);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, -2);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, -3);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, -4);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, -5);
    //MPowerPlayer
    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, 38);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, 40);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, 37);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, 39);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, 10);
    //for motorola
    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, KEY_NUM2);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, KEY_NUM8);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, KEY_NUM4);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, KEY_NUM6);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, KEY_NUM5);

    mapComponent.setZoomLevelIndicator(new DefaultZoomIndicator(0, 1));

    mapComponent.setMapListener(this);
    mapComponent.setCursor(new DefaultCursor(0xFFFF0000));
    mapComponent.startMapping();

  }

  protected void paint(final Graphics g) {
    if (mapComponent == null) {
      return;
    }

    mapComponent.paint(g);
  }

  public void deinitialize() {
    mapComponent.stopMapping();
  }

  public void mapClicked(final WgsPoint p) {
  }

  public void mapMoved() {
    System.out.println("center "+mapComponent.getCenterPoint());
    if (!sm.isInitializeConf()){
      System.out.println("ZoomLevels len: "+sm.getSupportedZoomLevels().length);
      for (int i=0;i<sm.getSupportedZoomLevels().length;i++){
        System.out.println("  ZoomLevel "+sm.getSupportedZoomLevels()[i]);
      }
    }
  }

  public void needRepaint(final boolean mapIsComplete) {
    repaint();
  }

  protected void keyPressed(final int keyCode) {
    if (mapComponent != null) {
      mapComponent.keyPressed(keyCode);
    }
  }

  protected void keyReleased(final int keyCode) {
    if (mapComponent != null) {
      mapComponent.keyReleased(keyCode);
    }
  }
}
